
//Tinh thoi gian tra ao dua vao so ngay lam ao da chon
function thoigian(){ 
    //alert('abc');
    var myDate = new Date()
    var tg = parseInt(document.getElementById('lamnhanh').value,10);
    myDate.setDate(myDate.getDate()+ tg -2 )
    var month1 = myDate.getMonth() + 1
    var day1 = myDate.getDate()
    var year1 = myDate.getFullYear()
    //alert(year1 + "-" + month1 + "-" + day1);
    //document.write(tg);
    document.getElementById('ngaytra').value = year1 + "-" + month1 + "-" + day1;   
}

//Tinh tien don hang
var numbers = new Array(' không ',' một ',' hai ',' ba ',' bốn ',' năm ',' sáu ',' bảy ',' tám ',' chín ');
var money = new Array('', ' nghìn', ' triệu', ' tỷ', ' nghìn tỷ', ' triệu tỷ');

function num2three(baso) {
    var tram;
    var chuc;
    var donvi;
    var ans = '';
    tram = parseInt(baso / 100);
    chuc = parseInt((baso % 100) / 10);
    donvi = baso % 10;
    if (tram == 0 && chuc == 0 && donvi == 0) return '';
    if (tram != 0) {
        ans += numbers[tram] + ' trăm ';
        if ((chuc == 0) && (donvi != 0)) ans += ' linh ';
    }
    if ((chuc != 0) && (chuc != 1)) {
        ans += numbers[chuc] + ' mươi';
		if ((chuc == 0) && (donvi != 0)) ans = ans + ' linh ';
    }
    if (chuc == 1) ans += ' mười ';
    switch (donvi) {
        case 1:
            if ((chuc != 0) && (chuc != 1))
				ans += ' mốt ';
            else
                ans += numbers[donvi];
            break;
        case 5:
            if (chuc == 0) 
                ans += numbers[donvi];
            else
				ans += ' lăm ';
            break;
        default:
            if (donvi != 0) ans += numbers[donvi];
			break;
        }
    return ans;
}
 
function num2string(allmoney) {
    var lan = 0;
    var i = 0;
    var so = 0;
    var ans = '';
    var tmp = '';
    var pos = new Array();
    if (allmoney > 0)
		so = allmoney;
	else
        so = -allmoney;
    pos[5] = Math.floor(so / 1000000000000000);
    if (isNaN(pos[5])) pos[5] = '0';
    so = so - parseFloat(pos[5].toString()) * 1000000000000000;
    pos[4] = Math.floor(so / 1000000000000);
    if(isNaN(pos[4])) pos[4] = '0';
    so = so - parseFloat(pos[4].toString()) * 1000000000000;
    pos[3] = Math.floor(so / 1000000000);
    if(isNaN(pos[3])) pos[3] = '0';
    so = so - parseFloat(pos[3].toString()) * 1000000000;
    pos[2] = parseInt(so / 1000000);
    if(isNaN(pos[2])) pos[2] = '0';
    pos[1] = parseInt((so % 1000000) / 1000);
    if(isNaN(pos[1])) pos[1] = '0';
    pos[0] = parseInt(so % 1000);
    if(isNaN(pos[0])) pos[0] = '0';
    if (pos[5] > 0) lan = 5;
    else if (pos[4] > 0) lan = 4;
    else if (pos[3] > 0) lan = 3;
    else if (pos[2] > 0) lan = 2;
    else if (pos[1] > 0) lan = 1;
    else lan = 0;
    for (i = lan; i >= 0; i--) {
       tmp = num2three(pos[i]);
       ans += tmp;
       if (pos[i] > 0) ans += money[i];
       if ((i > 0) && (tmp.length > 0)) ans += ',';
    }
    if (ans.substring(ans.length - 1) == ',') ans = ans.substring(0, ans.length - 1);
	return ans.substring(1, ans.length);
}

function refer(x) {
	from = document.getElementById(x);
	to = document.getElementById('for_' + x);
	to.innerHTML = num2string(parseInt(from.value));
}

function input2string(x) {
	from = document.getElementById(x);
	to = document.getElementById('for_' + x);
	to.innerHTML = num2string(parseInt(from.value));
	calc_price();
}

function calc_price() {
	sum_sum = document.getElementById('sum_sum');
	price = document.getElementById('price');
	sum = document.getElementById('sum');
	mortgage = document.getElementById('mortgage');
	remain = document.getElementById('remain');
	sum.value = sum_sum.value * price.value;
	remain.value = sum.value - mortgage.value;
	refer('sum');
	refer('remain');
}

//Tinh so luong ao quy doi tu kg vai san xuat
function tinhquydoi(){
    //alert('abc');
    a = parseInt(document.getElementById('khoiluong').value,10);
	b = parseInt(document.getElementById('tilequydoi').value,10);
	document.getElementById('soluong').value = parseInt(a * b);
}

//Ham tinh so luong size Nam + Nu check voi so luong dang ki ban dau
function calc_size() {
	sm = parseInt(document.getElementById('sizenams').value,10);
	mm = parseInt(document.getElementById('sizenamm').value,10);
	lm = parseInt(document.getElementById('sizenaml').value,10);
	xlm = parseInt(document.getElementById('sizenamxl').value,10);
	xxlm = parseInt(document.getElementById('sizenamxxl').value,10);
	xxxlm = parseInt(document.getElementById('sizenamxxxl').value,10);
	xxxxlm = parseInt(document.getElementById('sizenamxxxxl').value,10);
	sf = parseInt(document.getElementById('sizenus').value,10);
	mf = parseInt(document.getElementById('sizenum').value,10);
	lf = parseInt(document.getElementById('sizenul').value,10);
	xlf = parseInt(document.getElementById('sizenuxl').value,10);
	xxlf = parseInt(document.getElementById('sizenuxxl').value,10);
	xxxlf = parseInt(document.getElementById('sizenuxxxl').value,10);
	xxxxlf = parseInt(document.getElementById('sizenuxxxxl').value,10);
	m = document.getElementById('sum_m').value = parseInt(sm + mm + lm + xlm + xxlm + xxxlm + xxxxlm);
	f = document.getElementById('sum_f').value = parseInt(sf + mf + lf + xlf + xxlf + xxxlf + xxxxlf);
	document.getElementById('sum_sum').value = parseInt(m + f);
    sum = parseInt(m + f);
    soluong = parseInt(document.getElementById('soluong').value);
    if (sum != soluong)
        {document.getElementById('dungsai').value = 'Số lượng chưa chính xác !';}
    else document.getElementById('dungsai').value = 'OK số lượng chuẩn rồi đó !';
	calc_price();
}

//An hien cac tuy chon chi tiet them
function disable()
{
    if (document.getElementById('comu').checked == true) document.getElementById('maumu').style.visibility='visible';
        else    document.getElementById('maumu').style.visibility='hidden';
    if (document.getElementById('taykhacmau').checked == true) document.getElementById('mautayao').style.visibility='visible';
        else    document.getElementById('mautayao').style.visibility='hidden';
    if (document.getElementById('co').checked == true) 
            {   document.getElementById('chitietthemco').style.visibility='visible';
                document.getElementById('mauchitietthemco').style.visibility='visible';}
        else    {   document.getElementById('chitietthemco').style.visibility='hidden';
                document.getElementById('mauchitietthemco').style.visibility='hidden';}
    if (document.getElementById('tay').checked == true) 
            {   document.getElementById('chitietthemtay').style.visibility='visible';
                document.getElementById('mauchitietthemtay').style.visibility='visible';}
        else    {   document.getElementById('chitietthemtay').style.visibility='hidden';
                document.getElementById('mauchitietthemtay').style.visibility='hidden';}
    if (document.getElementById('gau').checked == true) 
            {   document.getElementById('chitietthemgau').style.visibility='visible';
                document.getElementById('mauchitietthemgau').style.visibility='visible';}
        else    {   document.getElementById('chitietthemgau').style.visibility='hidden';
                document.getElementById('mauchitietthemgau').style.visibility='hidden';}
    
        var selectmenu=document.getElementById("kieuco")
        selectmenu.onclick=function(){ //run some code when "onchange" event fires
         var chosenoption=this.options[this.selectedIndex] //this refers to "selectmenu"
         if (chosenoption.value!="0"){
                document.getElementById('chitietcobe').style.height='80px';
                document.getElementById('chitietcobe').style.width='200px';
                document.getElementById('chitietcobe').style.overflow='hidden';}
         else    {
                //alert('abc');
                document.getElementById('chitietcobe').style.height='0px';
                document.getElementById('chitietcobe').style.width='0px';
                document.getElementById('chitietcobe').style.overflow='hidden';}
        }                
}


//Phan tach so tien thanh dang 3 chu so mot
function executeComma(event) {
        //alert(event.keyCode);
        if ((event.keyCode >= 96 && event.keyCode <= 105)) {
                executeComma2();}
        else if (event.keyCode >= 48 && event.keyCode <= 57) {
                executeComma2();}
        else if (event.keyCode == 8 || event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9) {
                executeComma2();}
        else {
                alert("Giá trị nhập vào là một số!")
                document.themdonhang.dattruoc.value = "";
        calc_price();
        }

}
function executeComma2() {
        temp = document.themdonhang.dattruoc.value;
        /*for (i=0;i<temp.length;i++) {
                if (temp.charAt(i) == ',') {
                        temp.charAt(i) = '';
                }
        }*/
        for (i=0;i<temp.length;i++) {
               for (k=i;k<temp.length;k++) {
                        if (temp.charAt(k) == ',') {
                                temp = temp.replace(',','');
                        }
                }
        }
        var j = 0;
        var s = "";
        var s1 = "";
        var s2 = "";
        for (i=temp.length-1;i>=0;i--) {
                j = j+1;
                if (j == 3) {
                        j = 0;
                        s1 = temp.substring(0,i);
                        s2 = temp.substring(i,i+3);
                       s = "," + s2 + s;
                }
        }
        if (s1.length > 0) {
                //alert(s1.length);
                s = s1 + s;
                document.themdonhang.dattruoc.value = s;
        }else if (s.length > 0 && s2.length > 0){
                document.themdonhang.dattruoc.value = s.substring(1,s.length);
        }

}


//LAY NGAY THANG HIEN TAI VAO TEXT BOX GUI DON HANG
    // Created by: Jean P. May, Jr. | http://www.wideopenwest.com/~thebearmay
    // This script downloaded from www.JavaScriptBank.com

function autoDate () {
    	var tDay = new Date();
    	var tMonth = tDay.getMonth()+1;
    	var tDate = tDay.getDate();
    	if ( tMonth < 10) tMonth = "0"+tMonth;
    	if ( tDate < 10) tDate = "0"+tDate;
    	document.getElementById("tDate").value = tDay.getFullYear()+"-"+tMonth+"-"+tDate;
     }
    
    // Multiple onload function created by: Simon Willison
    // http://simonwillison.net/2004/May/26/addLoadEvent/
function addLoadEvent(func) {
      var oldonload = window.onload;
      if (typeof window.onload != 'function') {
        window.onload = func;
      } else {
        window.onload = function() {
          if (oldonload) {
            oldonload();
          }
          func();
        }
      }
    }

addLoadEvent(function() {
  autoDate();
});



//Ajax suggestions Ten khach hang
	function lookup(inputString) {
		if(inputString.length == 0) {
			// Hide the suggestion box.
			$('#suggestions').hide();
		} else {
			$.post("rpc.php", {queryString: ""+inputString+""}, function(data){
				if(data.length >0) {
					$('#suggestions').show();
					$('#autoSuggestionsList').html(data);
				}
			});
		}
	} // lookup
	
	function fill(thisValue) {
		$('#inputString').val(thisValue);
		setTimeout("$('#suggestions').hide();", 200);
	}